﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CPP.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CPPProject.CPP
{
    public class CPPSequenceAppService : AsyncCrudAppService<CPPSequence, CPPSequenceDto, int, PagedResultRequestDto, CreateCPPSequenceDto, UpdateCPPSequenceDto>, ICPPSequenceAppService
    {
        private readonly IObjectMapper _objectMapper;
        public CPPSequenceAppService(
            IRepository<CPPSequence, int> repository,
            IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        /// <summary>
        /// 根绝标识查询编码
        /// </summary>
        /// <param name="seqName">序列号标识</param>
        /// <returns></returns>
        private async Task<CPPSequenceDto> GetCPPSequence(string seqName)
        {
            var exp = await Repository.GetAll().Where(o => o.Seq_Name== seqName).FirstOrDefaultAsync();
            return _objectMapper.Map<CPPSequenceDto>(exp);
        }

        /// <summary>
        /// 重写修改方法
        /// </summary>
        /// <param name="id"></param>
        /// <param name="LastValue"></param>
        /// <returns></returns>
        private Boolean UpdateLastValue(int id, int LastValue)
        {
            //根绝ID获取到这一条的原始信息
            CPPSequence OBJ =  Repository.GetAll().Where(o =>o.Id==id).FirstOrDefault();
            //修改需要修改的字段
            OBJ.Last_Value = LastValue;
            //传入更改
            var lists =  Repository.Update(OBJ);
            //判断返回
            if (lists.Id > 0)
            {
                return true;
            }
            else{
                return false;
            }

        }

        /// <summary>
        /// 生成序列号
        /// </summary>
        /// <param name="seqName">序列号标识 cCarrierCode</param>
        /// <param name="active">生成后是否更新值 默认true</param>
        /// <returns></returns>
        public string GetRequestNo(string seqName, bool active = true)
        {
            lock (seqName)
            {
                var exp = Repository.GetAll().Where(o => o.Seq_Name == seqName).FirstOrDefault();
                CPPSequenceDto cPPSequence=_objectMapper.Map<CPPSequenceDto>(exp);

                int LastValue = cPPSequence.Step + cPPSequence.Last_Value; // Convert.ToInt32(listSquence["Step"]) + Convert.ToInt32(listSquence["Last_Value"]);
                string strLastValue = cPPSequence.Formatter + LastValue.ToString();// listSquence["Formatter"].ToString() + LastValue.ToString();
                int FormatterLength = cPPSequence.Formatter.ToString().Length;// listSquence["Formatter"].ToString().Length;//8
                string nextSequence = cPPSequence.Leading_Str + strLastValue.Substring(strLastValue.Length - FormatterLength, FormatterLength); // listSquence["Leading_Str"].ToString() + strLastValue.Substring(strLastValue.Length - FormatterLength, FormatterLength);
                if (active)
                {
                    UpdateLastValue(exp.Id, LastValue);
                    //Db.ExecuteNonQuery("Update T_Sys_Sequence Set Last_Value = @Last_Value Where Seq_Name = @Seq_Name AND nCompanyID=@nCompanyID",
                    //     new
                    //     {
                    //         Last_Value = LastValue,
                    //         Seq_Name = apprSeqName,
                    //         nCompanyID = nCompanyID
                    //     }
                    //    );
                }
                return nextSequence;
            }
        }


        
    }
}
